<?php


namespace App\Api\Controllers;


use App\Models\UserCommnet;
use App\Models\UserFan;
use App\Models\UserFav;
use App\Models\UserPost;
use App\Models\UserReply;
use App\Models\UserUpLog;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PostController extends Controller
{
    //图文
    public function addArticle(Request $request)
    {
        $user = auth()->user();
        $input = $request->only([
            'title',
            'content',
            'imgs',
        ]);
        $validator = Validator::make($input,[
            'title' => 'required',
            'content' => 'required',
            'imgs' => 'required',
        ]);
        if($validator->fails()){
            return $this->responseFailed($validator->errors()->first());
        }
        $post = new UserPost();
        foreach($input as $key=>$val){
            $post->$key = $val;
        }
        $post->user_id = $user['id'];
        if ($post->save()) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }

    //图文列表
    public function index()
    {
        $user = auth()->user();
        $posts = UserPost::where('user_id', $user['id'])->paginate(10)->toArray();
        foreach ($posts['data'] as $key => &$val) {
            $user = User::find($val['user_id']);
            if ($user) {
                $val['user'] = $user->toArray();
            } else {
                unset($posts['data'][$key]);
            }
        }
        return $this->responseData($posts);
    }

    //图文详情
    public function detail(Request $request)
    {
        $user = auth()->user();
        if (!$request->post_id) {
            return $this->responseFailed('图文ID必传');
        }
        $post = UserPost::find($request->post_id);
        if (!$post) {
            return $this->responseFailed('图文内容不存在');
        }
        $post = $post->toArray();
        $user = User::find($post['user_id']);
        $is_each_follow = 0;
        if ($user) {
            $post['user'] = $user;
            $where = [
                ['user_id', '=', $user['id']],
                ['follow_id', '=', $user['follow_id']],
            ];
            $userFan = UserFan::where($where)->first();
            if ($userFan) {
                $is_each_follow = $userFan['status'] == 1?1:0;
            }
        }
        $post['is_each_follow'] = $is_each_follow; //判断是否是已经关注状态
        $post['imgs'] = json_decode($post['imgs'], true);
        $where = [
            ['post_id', '=', $request->post_id],
        ];
        $commnets = UserCommnet::where($where)
            ->paginate(10)
            ->toArray();
        foreach ($commnets['data'] as $key => &$val) {
            $user = User::find($val['user_id']);
            if($user){
                $val['user'] = $user->toArray();
            } else {
                unset($commnets['data'][$key]);
            }
        }
        $post['comments'] = $commnets;
        return $this->responseData($post);
    }

    //图文点赞
    public function upPost(Request $request)
    {
        $user = auth()->user();
        $input = $request->only([
            'id',
        ]);
        $validator = Validator::make($input, [
            'id' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->responseFailed($validator->errors()->first());
        }
        $where = [
            ['post_id' , '=', $request->id],
            ['user_id' , '=', $user['id']],
        ];
        $upLog = UserUpLog::where($where)->first();
        if ($upLog) {
            return $this->responseFailed('已经点赞');
        }
        $post = UserPost::find($request->id);
        if (!$post) {
            return $this->responseFailed('图文不存在');
        }
        $uplog = new UserUpLog();
        $uplog->user_id = $user['id'];
        $uplog->post_id = $request->id;
        if ($uplog->save() && $post->increment('up_num')) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }

    //图文评论
    public function addComment(Request $request)
    {
        if (!$request->id) {
            return $this->responseFailed('ID必传');
        }
        if (!$request->commnet) {
            return $this->responseFailed('评论内容不能为空');
        }
        $post = UserPost::find($request->id);
        if (!$post) {
            return $this->responseFailed('图文不存在');
        }
        $post->commets_num = $post['commets_num'] + 1;
        $user = auth()->user();
        $commnet = new UserCommnet();
        $commnet->user_id = $user['id'];
        $commnet->post_id = $request->id;
        $commnet->commnet = $request->commnet;
        if ($commnet->save() &&   $post->save()) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }

    //图文评论点赞
    public function addCommnetUp(Request $request)
    {
        if (!$request->commnet_id) {
            return $this->responseFailed('ID必传');
        }
        $commnet = UserCommnet::find($request->commnet_id);
        if (!$commnet) {
            return $this->responseFailed('评论不存在');
        }
        $user = auth()->user();
        $where = [
            ['commnet_id' , '=', $request->commnet_id],
            ['user_id' , '=', $user['id']],
        ];
        $upLog = UserUpLog::where($where)->first();
        if ($upLog) {
            return $this->responseFailed('已经点赞');
        }
        $uplog = new UserUpLog();
        $uplog->user_id = $user['id'];
        $uplog->commnet_id = $request->commnet_id;
        if ($uplog->save() && $commnet->increment('up_num')) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }

    //评论添加回复
    public function addReply(Request $request)
    {
        $user = auth()->user();
        if (!$request->commnet_id) {
            return $this->responseFailed('ID必传');
        }
        if (!$request->reply_content) {
            return $this->responseFailed('回复内容不能为空');
        }
        $commnet = UserCommnet::find($request->commnet_id);
        if (!$commnet) {
            return $this->responseFailed('评论不存在');
        }
        $replay = new UserReply();
        $replay->replay_user_id = $user['id'];
        $replay->commnet_id = $request->commnet_id;
        $replay->reply_content = $request->reply_content;
        if ($replay->save()) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }

    //加收藏记录
    public function addfav(Request $request)
    {
        if (!$request->post_id) {
            return $this->responseFailed('post_id必传');
        }
        if (!$request->type) {
            return $this->responseFailed('type必传');
        }
        $user = auth()->user();
        $where = [
            ['user_id', '=', $user['id']],
            ['link_id', '=', $request->post_id],
        ];
        $fav = UserFav::where($where)->first();
        if ($fav) {
            return $this->responseFailed('已经收藏');
        }
        $post = UserPost::find($request->post_id);
        if (!$post) {
            return $this->responseFailed('图文不存在');
        }
        $fav = new UserFav();
        $fav->user_id = $user['id'];
        $fav->link_id = $request->post_id;
        $fav->type = $request->type;
        $post->fav_num = $post['fav_num'] + 1;
        if ($fav->save() && $post->save()) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }

    //转发次数增加
    public function addtrans(Request $request)
    {
        if (!$request->post_id) {
            return $this->responseFailed('post_id必传');
        }
        $post = UserPost::find($request->post_id);
        if (!$post) {
            return $this->responseFailed('图文不存在');
        }
        $post->trans_num = $post['trans_num'] + 1;
        if ($post->save()) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }
}